package com.aloda.service.impl;

import com.aloda.constant.TagConstant;
import com.aloda.entity.Tag;
import com.aloda.mapper.TagMapper;
import com.aloda.service.TagService;
import com.aloda.vo.TagListVO;
import com.aloda.vo.TagSimpleVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author aloda
 * @description 针对表【tag(标签表)】的数据库操作Service实现
 * @createDate 2024-10-21 08:36:37
 */
@Service
public class TagServiceImpl extends ServiceImpl<TagMapper, Tag> implements TagService {

    @Resource
    private TagMapper tagMapper;

    /**
     * 获取简单的标签列表
     *
     * @return
     */
    @Override
    public List<TagListVO> getTagList() {
        List<TagListVO> list = new ArrayList<>();
        // 1.查询所有已过审核标签
        LambdaQueryWrapper<Tag> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Tag::getAuth, TagConstant.TAG_NORMAL);
        List<Tag> tagList = tagMapper.selectList(lambdaQueryWrapper);
        // 2.找出其中的父标签
        tagList.stream().filter(tag -> tag.getIsParent() == TagConstant.TAG_IS_PARENT).forEach(parentTag -> {
            TagListVO tagListVO = new TagListVO();
            tagListVO.setText(parentTag.getTagName());
            List<TagSimpleVO> simpleVOList = tagList.stream()
                    .filter(tag -> Objects.equals(tag.getParentId(), parentTag.getId()))
                    .map(tag -> TagSimpleVO.builder().text(tag.getTagName()).id(tag.getId()).build())
                    .toList();
            tagListVO.setChildren(simpleVOList);
            list.add(tagListVO);
        });
        return list;
    }
}




